package com.erpanderp.wam.tm.view;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.primefaces.model.DualListModel;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.tm.model.entity.Band;
import com.erpanderp.wam.tm.model.entity.RRFSelectedSkillsList;
import com.erpanderp.wam.tm.model.entity.RecruitmentRequest;
import com.erpanderp.wam.tm.model.service.BandDAO;
import com.erpanderp.wam.tm.model.service.RRFDAO;
import com.erpanderp.wam.tm.model.service.RRFSelectedSkillsListDAO;
import com.erpanderp.wam.tm.model.service.SkillRatingDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.ValidationUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ViewScoped
@ManagedBean
public class CreateRRFBean implements Serializable { 
	
	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private WAMUser wamUser;
	private EmailUtil emailUtil;
	private String EMAIL_MESSAGE;
	private String referenceRRFCode;
	private String additionalSkill;
	private String enterNumber;
	
	private Map<String,String> clientMap = CommonLookup.getClient();
	private Map<String,String> interviewLevelMap = CommonLookup.getInterviewLevel();
	private Map<String,String> recruitmentModelMap = CommonLookup.getRecruitmentMode();
	private Map<String,String> recruitmentPriorityMap = CommonLookup.getRecruitmentPriority();	
	
	private DualListModel<String> primarySkills;
	List<String> primarySkillSouce = new ArrayList<String>();
	List<String> primarySkillTarget = new ArrayList<String>();
	List<String> commonSkills = new ArrayList<String>();
	
    private Employee employee = new Employee();
    private Map<String, Long> bandsMap = new HashMap<String, Long>();
    private Map<String, Long> businessFunctionMap = new HashMap<String, Long>();
    private Map<String, Long> supportFunctionMap = new HashMap<String, Long>();
    private Map<String, String> technologiesMap = new HashMap<String, String>();
    private Map<String, Long> recruitersMap = new HashMap<String, Long>(); //
    private Map<String, Long> interviewersMap = new HashMap<String, Long>();
    private Map<String, Long> interviewers2Map = new HashMap<String, Long>();
    private List<Band> bands = new ArrayList<Band>();
    private List<Band> businessFunctions = new ArrayList<Band>();
    private List<Band> supportFunctions = new ArrayList<Band>();
    private List<SAPERPModule> technologies = new ArrayList<SAPERPModule>();
    private List<Employee> recruiters = new ArrayList<Employee>();
    private List<Employee> interviewers = new ArrayList<Employee>();
    
    private RRFDAO rrfDAO = new RRFDAO();
    private BandDAO bandDAO=new BandDAO();
    private EmployeeAndReportingDAO employeeAndReportingDAO=new EmployeeAndReportingDAO();
    private RRFSelectedSkillsListDAO rrfSelectedSkillsListDAO=new RRFSelectedSkillsListDAO();
    private SAPERPModuleDAO saperpModuleDAO = new SAPERPModuleDAO();
    private SkillRatingDAO skillRatingDAO = new SkillRatingDAO();
    
    private String createDate;
    private String selectedBusinessFunction;
    private String selectedSupportFunction;
    private String selectedTechnology;
    private Long selectedRecruiterId;
    private String selectedInterviewLevel= CommonLookup.SINGLE_LEVEL_VALUE;
    private Long selectedInterviewerOne;
    private Long selectedInterviewerTwo;
    private String selectedRecruitmentMode;
    private String selectedRecruitmentPriority;
    private String selectedClient;
	private String employeeId;
	private String dispalyCenterHead;
	private Long selectedBandId;	
	private String secondarySkills;
	private boolean FORM_VALID;
	private String rrfCode;
	boolean rrfExists = true;
	
	private RecruitmentRequest recruitmentRequest = new RecruitmentRequest();
	private RRFSelectedSkillsList rrfSelectedSkillsList=new RRFSelectedSkillsList();
	private EmployeeAndReporting employeeAndReporting =new EmployeeAndReporting();
	
	private Date date,dateAfterTwoMnths;
	
    public CreateRRFBean(){
    	 
    	setWamUser((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
    	
    	rrfCode=CommonLookup.getAutoGeneratedRRFCode(wamUser.getEmployee().getEmployeeCode());
    	
    	Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formate= new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
		String toDay = formate.format(currentDate.getTime());
		
		//get after two months
		currentDate.add(Calendar.MONTH,2);
		
		
		try {
			date = (Date)formate.parse(toDay);
			dateAfterTwoMnths = (Date)formate.parse(formate.format(currentDate.getTime()));
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		
		setCreateDate(toDay);
		setRrfCode(rrfCode);
		setEmployeeId(wamUser.getEmployee().getEmployeeCode());
		employee.setFirstName(wamUser.getEmployee().getFirstName());
		employee.setLastName(wamUser.getEmployee().getLastName());
		
		employeeAndReporting = employeeAndReportingDAO.getEmployeeReportingDetail(wamUser.getEmployee().getEmployeeId());
		
		setDispalyCenterHead(employeeAndReporting.getCenterHead().getFirstName()+" "+employeeAndReporting.getCenterHead().getLastName());
		
		bands = rrfDAO.getAllBands();
		for (Band band : bands) {
			bandsMap.put(band.getBand(), band.getBandId());
		}
		
		businessFunctions = rrfDAO.getAllBands();
		for (Band businessFunction : businessFunctions) {
			businessFunctionMap.put(businessFunction.getBusinessFunction(), businessFunction.getBandId());
		}
		
		supportFunctions = rrfDAO.getAllBands();
		for (Band band : supportFunctions) {
			supportFunctionMap.put(band.getSupportFunction(), band.getBandId());
		}		
				
		technologies = saperpModuleDAO.getSAPERPModules();
		for (SAPERPModule saperpModule : technologies) {
			technologiesMap.put(saperpModule.getDisplayName(),
					saperpModule.getDisplayName());
			if(selectedTechnology == null)
				selectedTechnology = saperpModule.getDisplayName();
		}
		
		recruiters=rrfDAO.getAllRecruiters();
		for (Employee recruiter : recruiters) {
			recruitersMap.put(recruiter.getFirstName()+" "+recruiter.getLastName(), recruiter.getEmployeeId());
		}
		
		changeInterviewerListener(null);
		
		interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
		for (Employee interviewers1List : interviewers) {
			interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());
		}	
	   commonSkills = skillRatingDAO.getCommonSkills();
	   SAPERPModule technology = rrfDAO.getTechnologyByTechnologyName(getSelectedTechnology());
       primarySkillSouce = rrfDAO.getSkillsByTechnology(technology.getSAPERPModuleId());
	   primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget);  	
		
	}
	
	public void changeInterviewerListener(AjaxBehaviorEvent e) {
	
		interviewersMap.clear();
		interviewers2Map.clear();
		
				
		if (selectedTechnology == null || selectedTechnology == "") {
			System.out.println("No selected Technology==="+selectedTechnology);
		}else{
			
			if(selectedInterviewLevel.equalsIgnoreCase("Single Level")){
				System.out.println("INSIDE SINGLE LEVEL LOOP :::::::::::::::::");
				interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
				System.out.println("interviewers Size Bean :::::::::::::::::"+interviewers.size());
				for (Employee interviewers1List : interviewers) {
					interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());					
				}	
			}
			if(selectedInterviewLevel.equalsIgnoreCase("Two Level")){
				System.out.println("INSIDE TWO LEVEL LOOP :::::::::::::::::");
				interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
				for (Employee interviewers1List : interviewers) {
					interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());
				}	
				
				for (Employee interviewers2List : interviewers) {
					interviewers2Map.put(interviewers2List.getFirstName()+" "+interviewers2List.getLastName(), interviewers2List.getEmployeeId());					
				}					
			}
			interviewersMap.put(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName(), wamUser.getEmployee().getEmployeeId());
			interviewers2Map.put(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName(), wamUser.getEmployee().getEmployeeId());
			
			SAPERPModule technology = rrfDAO.getTechnologyByTechnologyName(selectedTechnology);
			primarySkillSouce = rrfDAO.getSkillsByTechnology(technology.getSAPERPModuleId());			
			primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget);  	
		}

	}
	
	//ADD Extra additional skill
	public void addExtraSkill(ActionEvent e){
		System.out.println("Inside action==============");
		System.out.println("Inside action=========111111111====="+additionalSkill);
		primarySkillSouce.add(additionalSkill);
		primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget);
		setAdditionalSkill(" ");
	}
	
	//SAVE RRF REQUEST
	public void saveRRFRequest(ActionEvent e){	

			rrfExists = rrfDAO.isRRFCodeExistOrNot(rrfCode);
			
			if (!rrfExists){                  
				rrfCode=CommonLookup.getAutoGeneratedRRFCode(wamUser.getEmployee().getEmployeeCode());								
			}	
			
			SAPERPModule technology = rrfDAO.getTechnologyByTechnologyName(selectedTechnology);
			Band band = rrfDAO.getBandByBandId(selectedBandId);
			Employee recruiter = rrfDAO.getRecruiterByRecruiterId(selectedRecruiterId);
			Employee interviewer1 = rrfDAO.getInterviewerByInterviewerId(selectedInterviewerOne);
			Employee interviewer2=null;
			
			if(selectedInterviewerTwo != null || selectedInterviewerTwo != 0){
			    interviewer2 = rrfDAO.getInterviewerByInterviewerId(selectedInterviewerTwo);
			}else{
				System.out.println("INTERVIEWER 2 IS NULL ::: "+ selectedInterviewerTwo);
				interviewer2=null;
				
			}
			
			 List<String> targetList = new ArrayList<String>();
			 targetList = primarySkills.getTarget();
			 String targetSkills="";			 
			  for(String pskill : targetList){				  
				  targetSkills += pskill;
				  targetSkills += ", ";				  
			 }

			  System.out.println("==========targetSkills============="+targetSkills);
			 if(targetSkills != null){ 
				 targetSkills = targetSkills.substring(0,targetSkills.lastIndexOf(","));
			 }
			//Strore technolony related selected skills
			for(String selectedSkill:targetList){
				System.out.println(" SELECTED SKILLS ADDED==========="+selectedSkill);
				rrfSelectedSkillsList.setRecruitmentRequestCode(rrfCode);
				rrfSelectedSkillsList.setSkillName(selectedSkill);
				rrfSelectedSkillsList.setTechnology(technology.getDisplayName());
				rrfSelectedSkillsList.setCreateDate(new Date());
				rrfSelectedSkillsList.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				rrfSelectedSkillsList.setStatus(CommonLookup.STATUS_ACTIVE);			
				
			    rrfSelectedSkillsListDAO.createRRFSelectedSkills(rrfSelectedSkillsList);
			}
			//Strore common skills
			for(String commonSkill:commonSkills){
				rrfSelectedSkillsList.setRecruitmentRequestCode(rrfCode);
				rrfSelectedSkillsList.setSkillName(commonSkill);
				rrfSelectedSkillsList.setTechnology(technology.getDisplayName());
				rrfSelectedSkillsList.setCreateDate(new Date());
				rrfSelectedSkillsList.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				rrfSelectedSkillsList.setStatus(CommonLookup.STATUS_ACTIVE);			
				
			    rrfSelectedSkillsListDAO.createRRFSelectedSkills(rrfSelectedSkillsList);				
			}
			
			recruitmentRequest.setRecruitmentRequestCode(rrfCode);
			recruitmentRequest.setRequestDate(new Date());
			recruitmentRequest.setBand(band);		
			recruitmentRequest.setCenterHead(employeeAndReporting.getCenterHead());
			recruitmentRequest.setEmployee(wamUser.getEmployee());
			recruitmentRequest.setRecruiter(recruiter);
			recruitmentRequest.setInterviewer1(interviewer1);
			recruitmentRequest.setInterviewer2(interviewer2);
			recruitmentRequest.setTechnology(technology);
			recruitmentRequest.setOtherTechnology(selectedTechnology);
			recruitmentRequest.setInterviewLevel(selectedInterviewLevel);
			recruitmentRequest.setInterviewerFBLevel("Level1");
			recruitmentRequest.setClientDetails(selectedClient);
			recruitmentRequest.setRecruitmentMode(selectedRecruitmentMode);
			recruitmentRequest.setPrimarySkills(targetSkills);
			recruitmentRequest.setRecruitmentPriority(selectedRecruitmentPriority);
			recruitmentRequest.setRecritmentStatus("New Recruitment Request");
			recruitmentRequest.setFormSaveOrSubmit("Saved");
			
			recruitmentRequest.setCreateDate(new Date());
			recruitmentRequest.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			recruitmentRequest.setStatus(CommonLookup.STATUS_ACTIVE);
			
			rrfDAO.createRRF(recruitmentRequest);
			
			
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Your recruitment request saved successfully.", ""));
			recruitmentRequest = new RecruitmentRequest();
			
			//for clearing purpose			
			selectedBandId=null;
			selectedBusinessFunction="";
			selectedSupportFunction="";
			selectedClient="";
			selectedInterviewLevel="";
			selectedRecruitmentMode="";
			selectedRecruitmentPriority="";
			primarySkillTarget.clear();
			
			interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
			for (Employee interviewers1List : interviewers) {
				interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());
			}
			
		    primarySkillSouce = rrfDAO.getSkillsByTechnology(technology.getSAPERPModuleId());
			primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget); 
			
			referenceRRFCode =rrfCode;
			System.out.println("referenceRRFCode============"+referenceRRFCode);
			rrfCode=CommonLookup.getAutoGeneratedRRFCode(wamUser.getEmployee().getEmployeeCode());	
		
	}
	
	
	//SUBMIT RRF REQUEST
	public void submitRRFRequest(ActionEvent e){	
		validateForm();
		
		if(FORM_VALID){
			rrfExists = rrfDAO.isRRFCodeExistOrNot(rrfCode);
			
			if (!rrfExists){                  
				rrfCode=CommonLookup.getAutoGeneratedRRFCode(wamUser.getEmployee().getEmployeeCode());								
			}	
			
			SAPERPModule technology = rrfDAO.getTechnologyByTechnologyName(selectedTechnology);
			Band band = rrfDAO.getBandByBandId(selectedBandId);
			Employee recruiter = rrfDAO.getRecruiterByRecruiterId(selectedRecruiterId);
			Employee interviewer1 = rrfDAO.getInterviewerByInterviewerId(selectedInterviewerOne);
			Employee interviewer2=null;
			
			if(selectedInterviewerTwo != null || selectedInterviewerTwo != 0){
			    interviewer2 = rrfDAO.getInterviewerByInterviewerId(selectedInterviewerTwo);
			}else{
				System.out.println("INTERVIEWER 2 IS NULL ::: "+ selectedInterviewerTwo);
				interviewer2=null;
				
			}
			
			 List<String> targetList = new ArrayList<String>();
			 targetList = primarySkills.getTarget();
			 String targetSkills="";			 
			  for(String pskill : targetList){				  
				  targetSkills += pskill;
				  targetSkills += ", ";				  
			 }	
			targetSkills = targetSkills.substring(0,targetSkills.lastIndexOf(","));
					
			//Strore technolony related selected skills
			for(String selectedSkill:targetList){
				System.out.println(" SELECTED SKILLS ADDED==========="+selectedSkill);
				rrfSelectedSkillsList.setRecruitmentRequestCode(rrfCode);
				rrfSelectedSkillsList.setSkillName(selectedSkill);
				rrfSelectedSkillsList.setTechnology(technology.getDisplayName());
				rrfSelectedSkillsList.setCreateDate(new Date());
				rrfSelectedSkillsList.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				rrfSelectedSkillsList.setStatus(CommonLookup.STATUS_ACTIVE);			
				
			    rrfSelectedSkillsListDAO.createRRFSelectedSkills(rrfSelectedSkillsList);
			}
			//Strore common skills
			for(String commonSkill:commonSkills){
				rrfSelectedSkillsList.setRecruitmentRequestCode(rrfCode);
				rrfSelectedSkillsList.setSkillName(commonSkill);
				rrfSelectedSkillsList.setTechnology(technology.getDisplayName());
				rrfSelectedSkillsList.setCreateDate(new Date());
				rrfSelectedSkillsList.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
				rrfSelectedSkillsList.setStatus(CommonLookup.STATUS_ACTIVE);			
				
			    rrfSelectedSkillsListDAO.createRRFSelectedSkills(rrfSelectedSkillsList);				
			}
			
			recruitmentRequest.setRecruitmentRequestCode(rrfCode);
			recruitmentRequest.setRequestDate(new Date());
			recruitmentRequest.setBand(band);		
			recruitmentRequest.setCenterHead(employeeAndReporting.getCenterHead());
			recruitmentRequest.setEmployee(wamUser.getEmployee());
			recruitmentRequest.setRecruiter(recruiter);
			recruitmentRequest.setInterviewer1(interviewer1);
			recruitmentRequest.setInterviewer2(interviewer2);
			recruitmentRequest.setTechnology(technology);
			recruitmentRequest.setOtherTechnology(selectedTechnology);
			recruitmentRequest.setInterviewLevel(selectedInterviewLevel);
			recruitmentRequest.setInterviewerFBLevel("Level1");
			recruitmentRequest.setClientDetails(selectedClient);
			recruitmentRequest.setRecruitmentMode(selectedRecruitmentMode);
			recruitmentRequest.setPrimarySkills(targetSkills);
			recruitmentRequest.setRecruitmentPriority(selectedRecruitmentPriority);
			recruitmentRequest.setRecritmentStatus("New Recruitment Request");
			recruitmentRequest.setFormSaveOrSubmit("Submitted");
			
			recruitmentRequest.setCreateDate(new Date());
			recruitmentRequest.setCreatedBy(CommonLookup.CREATEDBY_DEVELOPER_TEXT);
			recruitmentRequest.setStatus(CommonLookup.STATUS_ACTIVE);
			
			rrfDAO.createRRF(recruitmentRequest);
			
			//MAIL TRIGGERING CODE
			System.out.println("AT CALLER METHOD: " + sendMessage());  //*******DONT REMOVE*******
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Your Recruitment Request created successfully.", ""));
			recruitmentRequest = new RecruitmentRequest();
			
			//for clearing purpose
			
			selectedBandId=null;
			selectedBusinessFunction="";
			selectedSupportFunction="";
			selectedClient="";
			selectedInterviewLevel="";
			//selectedTechnology="";
			//selectedInterviewerOne=null;
			//selectedInterviewerTwo=null;
			selectedRecruitmentMode="";
			selectedRecruitmentPriority="";
			primarySkillTarget.clear();
			
			interviewers = rrfDAO.getInterviewersByModule(selectedTechnology);
			for (Employee interviewers1List : interviewers) {
				interviewersMap.put(interviewers1List.getFirstName()+" "+interviewers1List.getLastName(), interviewers1List.getEmployeeId());
			}
			
		    primarySkillSouce = rrfDAO.getSkillsByTechnology(technology.getSAPERPModuleId());
			primarySkills = new DualListModel<String>(primarySkillSouce, primarySkillTarget); 
			
			referenceRRFCode =rrfCode;
			System.out.println("referenceRRFCode============"+referenceRRFCode);
			rrfCode=CommonLookup.getAutoGeneratedRRFCode(wamUser.getEmployee().getEmployeeCode());	
			
		 
		
			
		}else{
			System.out.println("DATA NOT SAVE MODULE...Try Again");
		}
		
	}
	
	//SENDING EMAIL 
	public String sendMessage(){
		Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
		String domainName = context.getInitParameter("domainName");
		String messageString ="";		
		
		//Employee vp = employeeAndReportingDAO.getEmployeeByDesignation(CommonLookup.DESIGNATION_VICE_PRESIDENT_VALUE);
		//Employee ch = employeeAndReportingDAO.getEmployeeByDesignation(CommonLookup.DESIGNATION_SENIOR_REGIONAL_MANAGER_LABEL);
		
		emailUtil = new EmailUtil();
		if (hostsMap.get(CommonLookup.DEV_EMAIL) == null) {
			System.out.println("PRODUCTION");
			String[] to = { recruitmentRequest.getRecruiter().getEmail() };
			String[] cc = { recruitmentRequest.getCenterHead().getEmail()};
			emailUtil.setRecieverAddresses(to);
			emailUtil.setCCAddresses(cc);
		} else {
			System.out.println("DEVELOPMENT");
			String[] to = { hostsMap.get(CommonLookup.DEV_EMAIL) };
			String[] cc = {hostsMap.get(CommonLookup.DEV_EMAIL), hostsMap.get(CommonLookup.DEV_EMAIL)};
			emailUtil.setRecieverAddresses(to);
			emailUtil.setCCAddresses(cc);
		}
		
		String requestURL = "tm/viewRRFByReportingManager.html?rrfCode=" + rrfCode+"&EMPID="+wamUser.getEmployeeCode()+"&recruiterId="+recruitmentRequest.getRecruiter().getEmployeeCode() ;
		String privateLink = "http://" + hostsMap.get(CommonLookup.PRIVATEHOST) + "/" + domainName + "/" + requestURL;
		String publicLink = "http://" + hostsMap.get(CommonLookup.PUBLICHOST) + "/"	+ domainName + "/" + requestURL;
		
		messageString = "Dear " + recruitmentRequest.getRecruiter().getFirstName() + " " + recruitmentRequest.getRecruiter().getLastName() + "" +
								",<br /><br />" +
								
								"<table style='border: 1px solid #d9d9d9;'>" +
						        "<tr> <td>Recruitment Request Id </td><td> : </td><td>" + rrfCode+ " </td></tr>" +
						        "<tr> <td>Technology</td><td> : </td> <td>" + recruitmentRequest.getTechnology().getDisplayName() + " </td></tr>" +						       
						        "</table><br/>"+							       
						        "<a style='color:#FF9933;font-weight: bold;' href=\""+
						        privateLink + "\">" + "Please click here to view RRF Request and upload profiles" + "</a /><br />"+
								"(Employees who are in Hyderabad office)"+"<br /><br />"+
								"<a style='color:#FF9933;font-weight: bold;' href=\""+
								publicLink + "\">" + "Please click here to view RRF Request and upload profiles" + "</a /><br />"+
								"(out side Hyderabad office)"+"<br /><br />"+
								
								"Thanks & Regards, <br />"+CommonLookup.WAM_DEVTEAM_SIGNATURE;
	
		emailUtil.setMessage(messageString);
		
		emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
		
		emailUtil.setSubject(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName() +"( "+rrfCode+" ) - Recruitment Request");
		
		SwingWorker worker = new SwingWorker<String, Object>() {
			@Override
			protected String doInBackground() throws Exception {
				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
				return null;
			}

			@Override
			protected void done() {
					System.out.println("DONE " + EMAIL_MESSAGE);		
			}
		};
		worker.execute();
		System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
		return EMAIL_MESSAGE;
	}
	
//Validations Test
	private void validateForm(){
		FORM_VALID = true;		
	
		if(ValidationUtil.isEmpty(selectedBandId != 0 ? selectedBandId.toString() : null , "Band", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty(selectedBusinessFunction, "Business Function", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}	  
	   if(ValidationUtil.isEmpty(recruitmentRequest.getNoofPositions(), "No of Positions", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}else{			
			
			try
			{
				int noOfPositions = 0;
				noOfPositions = Integer.parseInt(recruitmentRequest.getNoofPositions());
			}
			catch (NumberFormatException nfe)
			{
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_WARN, "Please enter numeric values for No of postions.", ""));
				FORM_VALID = false;
			}			
		}
	   if(ValidationUtil.isEmpty(selectedTechnology, "Technology", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}	  
	   if(ValidationUtil.isEmpty(recruitmentRequest.getRelaventExperienceFrom() != 0 ? recruitmentRequest.getRelaventExperienceFrom().toString() : null, "Relavent Experience From", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty(recruitmentRequest.getRelaventExperienceTo() != 0 ? recruitmentRequest.getRelaventExperienceTo().toString() : null , "Relavent Experience To", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}	 
	   if(ValidationUtil.isEmpty(recruitmentRequest.getSalaryRangeFrom() != 0 ? recruitmentRequest.getSalaryRangeFrom().toString() : null , "Salary Range From", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}else{
			if (ValidationUtil.isSalaryValid(recruitmentRequest.getSalaryRangeFrom().toString(), "CTC From", null,
					FacesContext.getCurrentInstance()) != null) {
				FORM_VALID = false;
			} 
		}
	   if(ValidationUtil.isEmpty(recruitmentRequest.getSalaryRangeTo() != 0 ? recruitmentRequest.getSalaryRangeTo().toString() : null , "Salary Range To", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}else{
			if (ValidationUtil.isSalaryValid(recruitmentRequest.getSalaryRangeTo().toString(), "CTC To", null,
					FacesContext.getCurrentInstance()) != null) {
				FORM_VALID = false;
			} 
		}
	   if(recruitmentRequest.getSalaryRangeFrom() > recruitmentRequest.getSalaryRangeTo()){
		   FORM_VALID = false;
		   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"CTC From should always less than CTC To", "")); 
	   }
	   if(ValidationUtil.isEmpty(recruitmentRequest.getJobLocation(), "	Job Location", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
	   if(ValidationUtil.isEmpty((recruitmentRequest.getRecritmentEndDate()!=null) ? recruitmentRequest.getRecritmentEndDate().toString():null, "Recruitment End Date", null, FacesContext.getCurrentInstance()) != null){
	
		   FORM_VALID = false;
	   }   
	  
	  if(recruitmentRequest.getRecritmentEndDate() != null){
		   if(recruitmentRequest.getRecritmentEndDate().before(date)){
			   FORM_VALID = false;
			   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN," Recruitment end date Should be greater than rrf create date", ""));
		   }
		   if(recruitmentRequest.getRecritmentEndDate().after(dateAfterTwoMnths)){
			   FORM_VALID = false;
			   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Recruitment end date should not be exceed 2 months (or) 60 Days", ""));
		   }
	  }
	  
	  List<String> targetList = new ArrayList<String>();
	  targetList = primarySkills.getTarget();
	  System.out.println("=============targetList.size()==============="+targetList.size());
	  if(targetList.size() == 0){
		  FORM_VALID = false;
		   FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Primary skills can not be empty", ""));
	  }
	  if(ValidationUtil.isEmpty(recruitmentRequest.getJobDescription(), "Job Description", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
	  }	
	 
		System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);		
	}
    
    //GENERATE SETTERS AND GETTERS	
	public String getCreateDate() {
		return createDate;
	}
	public Map<String, Long> getInterviewersMap() {
		return interviewersMap;
	}

	public void setInterviewersMap(Map<String, Long> interviewersMap) {
		this.interviewersMap = interviewersMap;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}
	
	public WAMUser getWamUser() {
		return wamUser;
	}
	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public String getEmployeeId() {
		return employeeId;
	}
	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}
	
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Long getSelectedBandId() {
		return selectedBandId;
	}
	public void setSelectedBandId(Long selectedBandId) {
		this.selectedBandId = selectedBandId;
	}

	public Map<String, Long> getBandsMap() {
		return bandsMap;
	}
	public void setBandsMap(Map<String, Long> bandsMap) {
		this.bandsMap = bandsMap;
	}
	
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}
	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public List<Band> getBands() {
		return bands;
	}
	public void setBands(List<Band> bands) {
		this.bands = bands;
	}

	public Map<String, Long> getBusinessFunctionMap() {
		return businessFunctionMap;
	}

	public void setBusinessFunctionMap(Map<String, Long> businessFunctionMap) {
		this.businessFunctionMap = businessFunctionMap;
	}

	public Map<String, Long> getSupportFunctionMap() {
		return supportFunctionMap;
	}

	public void setSupportFunctionMap(Map<String, Long> supportFunctionMap) {
		this.supportFunctionMap = supportFunctionMap;
	}

	public List<Band> getBusinessFunctions() {
		return businessFunctions;
	}

	public void setBusinessFunctions(List<Band> businessFunctions) {
		this.businessFunctions = businessFunctions;
	}

	public List<Band> getSupportFunctions() {
		return supportFunctions;
	}

	public void setSupportFunctions(List<Band> supportFunctions) {
		this.supportFunctions = supportFunctions;
	}

	public String getSelectedBusinessFunction() {
		return selectedBusinessFunction;
	}

	public void setSelectedBusinessFunction(String selectedBusinessFunction) {
		this.selectedBusinessFunction = selectedBusinessFunction;
	}

	public String getSelectedSupportFunction() {
		return selectedSupportFunction;
	}

	public void setSelectedSupportFunction(String selectedSupportFunction) {
		this.selectedSupportFunction = selectedSupportFunction;
	}

	public Map<String,String> getClientMap() {
		return clientMap;
	}

	public void setClientMap(Map<String,String> clientMap) {
		this.clientMap = clientMap;
	}

	public String getSelectedClient() {
		return selectedClient;
	}

	public void setSelectedClient(String selectedClient) {
		this.selectedClient = selectedClient;
	}

	public Map<String, String> getTechnologiesMap() {
		return technologiesMap;
	}

	public void setTechnologiesMap(Map<String, String> technologiesMap) {
		this.technologiesMap = technologiesMap;
	}

	public List<SAPERPModule> getTechnologies() {
		return technologies;
	}

	public void setTechnologies(List<SAPERPModule> technologies) {
		this.technologies = technologies;
	}

	public String getSelectedTechnology() {
		return selectedTechnology;
	}

	public void setSelectedTechnology(String selectedTechnologyId) {
		this.selectedTechnology = selectedTechnologyId;
	}

	public List<Employee> getRecruiters() {
		return recruiters;
	}

	public void setRecruiters(List<Employee> recruiters) {
		this.recruiters = recruiters;
	}

	public Map<String, Long> getRecruitersMap() {
		return recruitersMap;
	}

	public void setRecruitersMap(Map<String, Long> recruitersMap) {
		this.recruitersMap = recruitersMap;
	}

	public Map<String,String> getInterviewLevelMap() {
		return interviewLevelMap;
	}

	public void setInterviewLevelMap(Map<String,String> interviewLevelMap) {
		this.interviewLevelMap = interviewLevelMap;
	}

	public String getSelectedInterviewLevel() {
		return selectedInterviewLevel;
	}

	public void setSelectedInterviewLevel(String selectedInterviewLevel) {
		this.selectedInterviewLevel = selectedInterviewLevel;
	}

	public List<Employee> getInterviewers() {
		return interviewers;
	}

	public void setInterviewers(List<Employee> interviewers) {
		this.interviewers = interviewers;
	}

	public Long getSelectedInterviewerOne() {
		return selectedInterviewerOne;
	}

	public void setSelectedInterviewerOne(Long selectedInterviewerOne) {
		this.selectedInterviewerOne = selectedInterviewerOne;
	}

	public Long getSelectedInterviewerTwo() {
		return selectedInterviewerTwo;
	}

	public void setSelectedInterviewerTwo(Long selectedInterviewerTwo) {
		this.selectedInterviewerTwo = selectedInterviewerTwo;
	}

	public Map<String,String> getRecruitmentModelMap() {
		return recruitmentModelMap;
	}

	public void setRecruitmentModelMap(Map<String,String> recruitmentModelMap) {
		this.recruitmentModelMap = recruitmentModelMap;
	}

	public String getSelectedRecruitmentMode() {
		return selectedRecruitmentMode;
	}

	public void setSelectedRecruitmentMode(String selectedRecruitmentMode) {
		this.selectedRecruitmentMode = selectedRecruitmentMode;
	}

	public Map<String,String> getRecruitmentPriorityMap() {
		return recruitmentPriorityMap;
	}

	public void setRecruitmentPriorityMap(Map<String,String> recruitmentPriorityMap) {
		this.recruitmentPriorityMap = recruitmentPriorityMap;
	}

	public String getSelectedRecruitmentPriority() {
		return selectedRecruitmentPriority;
	}

	public void setSelectedRecruitmentPriority(
			String selectedRecruitmentPriority) {
		this.selectedRecruitmentPriority = selectedRecruitmentPriority;
	}

	public RecruitmentRequest getRecruitmentRequest() {
		return recruitmentRequest;
	}

	public void setRecruitmentRequest(RecruitmentRequest recruitmentRequest) {
		this.recruitmentRequest = recruitmentRequest;
	}

	public String getRrfCode() {
		return rrfCode;
	}

	public void setRrfCode(String rrfCode) {
		this.rrfCode = rrfCode;
	}

	public Long getSelectedRecruiterId() {
		return selectedRecruiterId;
	}

	public void setSelectedRecruiterId(Long selectedRecruiterId) {
		this.selectedRecruiterId = selectedRecruiterId;
	}
	
	public Map<String, Long> getInterviewers2Map() {
		return interviewers2Map;
	}

	public void setInterviewers2Map(Map<String, Long> interviewers2Map) {
		this.interviewers2Map = interviewers2Map;
	}

	public DualListModel<String> getPrimarySkills() {
		return primarySkills;
	}

	public void setPrimarySkills(DualListModel<String> primarySkills) {
		this.primarySkills = primarySkills;
	}

	public String getSecondarySkills() {
		return secondarySkills;
	}

	public void setSecondarySkills(String secondarySkills) {
		this.secondarySkills = secondarySkills;
	}

	public String getDispalyCenterHead() {
		return dispalyCenterHead;
	}

	public void setDispalyCenterHead(String dispalyCenterHead) {
		this.dispalyCenterHead = dispalyCenterHead;
	}

	public EmployeeAndReporting getEmployeeAndReporting() {
		return employeeAndReporting;
	}

	public void setEmployeeAndReporting(EmployeeAndReporting employeeAndReporting) {
		this.employeeAndReporting = employeeAndReporting;
	}

	public EmployeeAndReportingDAO getEmployeeAndReportingDAO() {
		return employeeAndReportingDAO;
	}

	public void setEmployeeAndReportingDAO(
			EmployeeAndReportingDAO employeeAndReportingDAO) {
		this.employeeAndReportingDAO = employeeAndReportingDAO;
	}

	public RRFSelectedSkillsListDAO getRrfSelectedSkillsListDAO() {
		return rrfSelectedSkillsListDAO;
	}

	public void setRrfSelectedSkillsListDAO(RRFSelectedSkillsListDAO rrfSelectedSkillsListDAO) {
		this.rrfSelectedSkillsListDAO = rrfSelectedSkillsListDAO;
	}

	public EmailUtil getEmailUtil() {
		return emailUtil;
	}

	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}

	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}

	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}

	public SAPERPModuleDAO getSaperpModuleDAO() {
		return saperpModuleDAO;
	}

	public void setSaperpModuleDAO(SAPERPModuleDAO saperpModuleDAO) {
		this.saperpModuleDAO = saperpModuleDAO;
	}

	public SkillRatingDAO getSkillRatingDAO() {
		return skillRatingDAO;
	}

	public void setSkillRatingDAO(SkillRatingDAO skillRatingDAO) {
		this.skillRatingDAO = skillRatingDAO;
	}

	public BandDAO getBandDAO() {
		return bandDAO;
	}

	public void setBandDAO(BandDAO bandDAO) {
		this.bandDAO = bandDAO;
	}

	public String getAdditionalSkill() {
		return additionalSkill;
	}

	public void setAdditionalSkill(String additionalSkill) {
		this.additionalSkill = additionalSkill;
	}

	public String getEnterNumber() {
		return enterNumber;
	}

	public void setEnterNumber(String enterNumber) {
		this.enterNumber = enterNumber;
	}


}
